// index.js
// 导入apis
import {getBanner,getSuggest} from '../../apis/index.js'

Page({
    data:{
        allMatch:[
            {keyword:'陈奕迅'},
            {keyword:'周杰伦'}
        ],
        searchValue:'',
        searchShow:false,
        userInfo:{
        avatarUrl:'https://img1.baidu.com/it/u=2250275304,3941496093&fm=253&fmt=auto&app=138&f=GIF?w=200&h=200',
        nickname:'立即登录'
    },//用户个人信息
        leftSelectShow:false,
        //页面初始信息
        banner:[
            {pic:"http://p1.music.126.net/uEolBJPrqg0XrxFmuSfrLw==/109951167364754313.jpg"},
            {pic:"http://p1.music.126.net/uEolBJPrqg0XrxFmuSfrLw==/109951167364754313.jpg"},
            {pic:"http://p1.music.126.net/uEolBJPrqg0XrxFmuSfrLw==/109951167364754313.jpg"}
        ],
        head:'https://img1.baidu.com/it/u=2250275304,3941496093&fm=253&fmt=auto&app=138&f=GIF?w=200&h=200',
        name:'立即登录'
    },
    // 用户点击了确定搜索
    searchOk(){
        // 首先拿到搜索内容
        var searchValue = this.data.searchValue;
        // 跳转到搜索页面
        if(searchValue != ''){
            wx.navigateTo({
              url: '/pages/search/search?keyword='+searchValue,
            })
        }
    },
    // 搜索内容发生变化
    searchChange(){
        var searchValue = this.data.searchValue;
        if(searchValue == ""){
            this.setData({searchShow:false});
        }else{
            this.setData({searchShow:true});
            var that = this;
            getSuggest(searchValue).then(function(res){
                that.setData({
                    allMatch:res.result.allMatch
                })
            })
        }
    },
    //页面跳转到登录页面
    jumpLogin(){
        wx.navigateTo({
          url: '/pages/login/login'
        })
    },
    //用于显示左侧的选择栏
    leftSelect(){
        this.setData({ leftSelectShow:true });
    },
    onSelectClose(){
        this.setData({ leftSelectShow:false });
    },
    onLoad(options){
        var that = this;
        //监听页面加载
        //请求会失败 在详情里有个不校验合法域名给勾上
        getBanner(2).then(function(res){
            that.setData({
                banner:res.banners
            })
        })
    },
    onShow(){
        var that = this;
        // 判断用户是否已经登录
        // 缓存中获取token
        wx.getStorage({
            key: 'userInfo',
            success (res){
                console.log(res.data)
                that.setData({
                    userInfo:res.data
                })
            }
        })
    }
})
